package org.bright.controller;

import org.bright.entity.Products;
import org.bright.entity.R;
import org.bright.enums.HttpEnum;
import org.bright.service.IProductsService;
import org.bright.util.FileUtil;
import org.bright.vo.ProductsDisplayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bright
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/products")
@CrossOrigin
public class ProductsController {
    /**
     * 服务对象
     */
    @Autowired
    IProductsService productsService;
    /**
     * 分页查询
     *
     * @param productName 筛选条件
     * @param pageNum      分页对象
     * @return 查询结果
     */
    @GetMapping
    public R queryByPage(String productName, String supplierName,Integer pageNum) {
        return R.success(productsService.queryByPage(productName,supplierName,pageNum));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        ProductsDisplayVO productsDisplayVO = productsService.queryById(id);
        try{
            if ( productsDisplayVO != null) {
                return R.success(productsDisplayVO);
            }
        }catch (Exception e){
            return R.fail(HttpEnum.WARN.getCode(),e.getMessage());
        }
        return R.fail(HttpEnum.WARN.getCode(),"查询失败");
    }

    /**
     * 新增数据
     *
     * @param products 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@Valid Products products, MultipartFile imageFile, HttpServletRequest request) {
        System.out.println(products.toString());
        String imagePath;
        try {
            imagePath = FileUtil.upload(request,imageFile);
            if (imagePath != null){
                products.setImageUrl(imagePath);
                if (productsService.insert(products) > 0) {
                    return R.success("新增成功");
                }
            }
        }catch (Exception e){
            return R.fail(HttpEnum.ERROR.getCode(), e.getMessage());
        }
        return R.fail(HttpEnum.ERROR.getCode(), "新增失败");
    }

    /**
     * 编辑数据
     *
     * @param products 实体
     * @return 编辑结果
     */
    @PostMapping("update")
    public R updateUsers(@Valid Products products, MultipartFile imageFile, HttpServletRequest request) {
        System.out.println(products.toString());
        String imagePath;
        try {
            imagePath = FileUtil.upload(request, imageFile);
            if (imagePath != null) {
                products.setImageUrl(imagePath);
            }else {
                String imagePath1 = productsService.selectImageUrl(products.getProductId());
                products.setImageUrl(imagePath1);
            }
            if (productsService.update(products) > 0) {
                return R.success("修改成功");
            }
        } catch (Exception e) {
            return R.fail(HttpEnum.ERROR.getCode(), e.getMessage());
        }
        return R.fail(HttpEnum.WARN.getCode(), "修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        try {
            if (productsService.deleteById(id) > 0) {
                return R.success("删除成功");
            }
        } catch (Exception e) {
            return R.fail(HttpEnum.ERROR.getCode(), e.getMessage());
        }
        return R.fail(HttpEnum.ERROR.getCode(), "删除失败");
    }
}
